package com.tianma315.web.procedures.controller;


import com.baomidou.mybatisplus.plugins.Page;


import com.tianma315.core.common.utils.Result;
import com.tianma315.core.procedures.domain.ProceduresDO;
import com.tianma315.core.procedures.service.ProceduresService;
import com.tianma315.core.procedures.vo.ProceduresVO;
import com.tianma315.web.common.base.BaseController;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 
 * <pre>
 * 溯源流程模板
 * </pre>
 * <small> 2019-08-13 16:34:19 | Lgc</small>
 */
@Controller
@RequestMapping("/procedures/procedures")
public class ProceduresController extends BaseController {

	@Autowired
	private ProceduresService proceduresService;

	/**
	 *
	 * @return 内部转发到溯源流程模版页面
	 */
	@GetMapping()
	@RequiresPermissions("procedures:procedures:procedures")
	String Procedures(){
	    return "procedures/procedures/procedures";
	}

	/**
	 * 将流程模板Id 通过model域传到绑定产品页面中去
	 * @param proceduresId 流程模板Id
	 * @param model 存储数据
	 * @return 内部转发到绑定产品页面
	 */
	@GetMapping("/bindProduct/{proceduresId}")
	String bindProduct(@PathVariable Integer proceduresId, Model model){
		model.addAttribute("proceduresId",proceduresId);
		return "procedures/procedures/procedures_product";
	}

	/**
	 * 分页模糊查询流程模板数据
	 * @param pageNumber 当前页码
	 * @param pageSize 页的大小
	 * @param proceduresDTO 流程模版实体类
	 * @return 返回封装了分页数据的Result对象
	 */
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("procedures:procedures:procedures")
	public Result<Page<ProceduresVO>> list(Integer pageNumber, Integer pageSize, ProceduresDO proceduresDTO){
//		proceduresDTO.setMerchantId(getMerchantId());
		// 查询列表数据
        Page<ProceduresVO> page = proceduresService.selectPage(pageNumber,pageSize,proceduresDTO);
        return Result.ok(page);
	}

	/**
	 *  查询出所有的流程模板数据
	 * @param proceduresDTO 根据条件MerchantId
	 * @return 返回数据结果对象
	 */
	@ResponseBody
	@GetMapping("/all")
	public Result<List<ProceduresDO>> list(ProceduresDO proceduresDTO){
//		proceduresDTO.setMerchantId(getMerchantId());
		// 查询列表数据
		List<ProceduresDO> list = proceduresService.selectList(proceduresDTO);
		return Result.ok(list);
	}

	/**
	 *
	 * @return 内部转发到添加页面
	 */
	@GetMapping("/add")
	@RequiresPermissions("procedures:procedures:add")
	String add(){
	    return "procedures/procedures/add";
	}

	/**
	 * 通过Id去查询数据然后回显数据到修改页面中去
	 * @param procedureId
	 * @param model 存储数据
	 * @return 内部转发到编辑页
	 */
	@GetMapping("/edit/{procedureId}")
	@RequiresPermissions("procedures:procedures:edit")
	String edit(@PathVariable("procedureId") Integer procedureId, Model model){
		ProceduresDO procedures = proceduresService.selectById(procedureId);
		model.addAttribute("procedures", procedures);
	    return "procedures/procedures/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("procedures:procedures:add")
	public Result<String> save( ProceduresDO procedures){
//		procedures.setMerchantId(getMerchantId());
		procedures.setCreateBy(getUserId());
		if(proceduresService.addProcedures(procedures)){
			return Result.ok();
		}
		return Result.fail();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("procedures:procedures:edit")
	public Result<String>  update( ProceduresDO procedures){
		proceduresService.updateProcedures(procedures);
		return Result.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("procedures:procedures:remove")
	public Result<String>  remove(Integer procedureId){
		if(proceduresService.deleteProcedures(procedureId)){
            return Result.ok();
		}
		return Result.fail();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("procedures:procedures:batchRemove")
	public Result<String>  remove(@RequestParam("ids[]") Integer[] procedureIds){
		for (Integer id:procedureIds) {
			proceduresService.deleteProcedures(id);
		}

//		proceduresService.deleteBatchIds(Arrays.asList(procedureIds));
		return Result.ok();
	}

	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/getProceduresDOByProductId")
	public Result<ProceduresDO> getProceduresDOByProductId( Integer productId){
		ProceduresDO proceduresDO = proceduresService.getProceduresDOByProductId(productId);
		return Result.ok(proceduresDO);
	}
}
